﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;

namespace Zero.UControl.Component
{
    /// <summary>
    /// CalendarDayItem.xaml 的交互逻辑
    /// </summary>
    public partial class CalendarDayItem : UserControl
    {
        private CalendarDay calendarDay;
        public CalendarDayItem()
        {
            InitializeComponent();
            Loaded += CalendarDayItem_Loaded;
        }


        public static readonly RoutedEvent DayClickedEvent = EventManager.RegisterRoutedEvent(
            nameof(DayClicked),
            RoutingStrategy.Bubble,
            typeof(RoutedEventHandler),
            typeof(CalendarDayItem));

        public event RoutedEventHandler DayClicked
        {
            add => AddHandler(DayClickedEvent, value);
            remove => RemoveHandler(DayClickedEvent, value);
        }

        private void CalendarDayItem_Loaded(object sender, RoutedEventArgs e)
        {
            calendarDay = DataContext as CalendarDay;
            if (calendarDay == null) return;

            if (calendarDay.IsToDay)
            {
                RootBorder.BorderThickness = new Thickness(2);
                RootBorder.BorderBrush = (SolidColorBrush)Resources["CurrentDayBorderColor"];
            }
        }

        private void RootBorder_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if(calendarDay == null) return;
            if (!calendarDay.IsCurrentMonth) return;
            // 将事件向上冒泡，传给父组件
            RaiseEvent(new RoutedEventArgs(DayClickedEvent, calendarDay));
        }
    }
}
